package com.ktjiaoyu.crm.web.controller;

import com.ktjiaoyu.crm.entity.BasDict;
import com.ktjiaoyu.crm.entity.Role;
import com.ktjiaoyu.crm.entity.Storage;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.service.IBasDictService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BasDictController {
    @Resource
    private IBasDictService basDictService;
    @RequestMapping(value = "/basd/list")
    public String list(Model model, Long dictId, String dictItem,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Sort sort = new Sort(Sort.Direction.ASC, "dictId");
        Pageable pageable = PageRequest.of(pageIndex-1, 5, sort);
        Page<BasDict> dictPage = basDictService.findServiceType(dictId,dictItem,pageable);
        model.addAttribute("dictPage", dictPage);
        model.addAttribute("dictId", dictId);
        model.addAttribute("dictItem", dictItem);
        return "basd/list";
    }
    @RequestMapping(value = "/basd/add")
    public String add(){
        return "basd/add";
    }

    @RequestMapping(value = "/basd/save")
    public String save(BasDict basDict){
        basDict.setDictValue(basDict.getDictItem());
        basDictService.saveServiceType(basDict);
        return "redirect:/basd/list";
    }
    @RequestMapping(value = "/basd/edit")
    public String edit(Long dictId, Model model){
       BasDict dict=basDictService.getDict(dictId);
       model.addAttribute("dict",dict);
        return "basd/edit";
    }
    @RequestMapping(value = "/basd/del")
    @ResponseBody
    public Map del(Long dictId){
        basDictService.delServiceType(dictId);
        Map map=new HashMap();
        map.put("delResult","true");
        return map;
    }


    @RequestMapping(value = "/basd/region_list")
    public String regionList(Model model, Long dictId, String dictItem,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Sort sort = new Sort(Sort.Direction.ASC, "dictId");
        Pageable pageable = PageRequest.of(pageIndex-1, 5, sort);
        Page<BasDict> dictPage = basDictService.findServiceType(dictId,dictItem,pageable);
        model.addAttribute("dictPage", dictPage);
        model.addAttribute("dictId", dictId);
        model.addAttribute("dictItem", dictItem);
        return "basd/region_list";
    }
    @RequestMapping(value = "/basd/region_add")
    public String regionAdd(){
        return "basd/region_add";
    }

    @RequestMapping(value = "/basd/region_save")
    public String regionSave(BasDict basDict){
        basDict.setDictValue(basDict.getDictItem());
        basDictService.saveServiceType(basDict);
        return "redirect:/basd/region_list";
    }

    @RequestMapping(value = "/basd/region_edit")
    public String regionEdit(Long dictId, Model model){
        BasDict dict=basDictService.getDict(dictId);
        model.addAttribute("dict",dict);
        return "basd/region_edit";
    }
    @RequestMapping(value = "/basd/cust_list")
    public String custList(Model model, Long dictId, String dictItem,
                           @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Sort sort = new Sort(Sort.Direction.ASC, "dictId");
        Pageable pageable = PageRequest.of(pageIndex-1, 5, sort);
        Page<BasDict> dictPage = basDictService.findServiceType(dictId,dictItem,pageable);
        model.addAttribute("dictPage", dictPage);
        model.addAttribute("dictId", dictId);
        model.addAttribute("dictItem", dictItem);
        return "basd/cust_list";
    }
    @RequestMapping(value = "/basd/cust_add")
    public String custAdd(){
        return "basd/cust_add";
    }

    @RequestMapping(value = "/basd/cust_save")
    public String custSave(BasDict basDict){
        basDict.setDictValue(basDict.getDictItem());
        basDictService.saveServiceType(basDict);
        return "redirect:/basd/cust_list";
    }

    @RequestMapping(value = "/basd/cust_edit")
    public String custEdit(Long dictId, Model model){
        BasDict dict=basDictService.getDict(dictId);
        model.addAttribute("dict",dict);
        return "basd/cust_edit";
    }
}
